package cn.topcodes.tcsf.app.service.impl;

import cn.topcodes.tcsf.admin.domain.entity.SystemProperty;
import cn.topcodes.tcsf.admin.service.SystemPropertyService;
import cn.topcodes.tcsf.app.domain.form.AppAboutSettings;
import cn.topcodes.tcsf.app.service.AppSettingsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Created by Unicorn on 2017/7/26.
 */
@Service("appSettingsService")
@Transactional(readOnly = true)
public class AppSettingsServiceImpl implements AppSettingsService {

    @Autowired
    private SystemPropertyService systemPropertyService;

    @Override
    public AppAboutSettings getAppAboutSettings() {
        AppAboutSettings appAboutSettings = new AppAboutSettings();
        String compnayName = this.systemPropertyService.findSysProperty(APP_SETTINGS_ABOUT_COMPANY_NAME, String.class, "");
        String compnayTel = this.systemPropertyService.findSysProperty(APP_SETTINGS_ABOUT_COMPANY_TEL, String.class, "");
        String compnayAddress = this.systemPropertyService.findSysProperty(APP_SETTINGS_ABOUT_COMPANY_ADDRESS, String.class, "");
        String compnayEmail = this.systemPropertyService.findSysProperty(APP_SETTINGS_ABOUT_COMPANY_EMAIL, String.class, "");
        String compnayUrl = this.systemPropertyService.findSysProperty(APP_SETTINGS_ABOUT_COMPANY_URL, String.class, "");
        appAboutSettings.setCompanyName(compnayName);
        appAboutSettings.setCompanyAddress(compnayAddress);
        appAboutSettings.setCompanyEmail(compnayEmail);
        appAboutSettings.setCompanyTel(compnayTel);
        appAboutSettings.setCompanyUrl(compnayUrl);
        return appAboutSettings;
    }

    @Override
    @Transactional(readOnly = false)
    public void setAppAboutSettings(AppAboutSettings settings) {
        this.systemPropertyService.modifySysProp(APP_SETTINGS_ABOUT_COMPANY_NAME, settings.getCompanyName());
        this.systemPropertyService.modifySysProp(APP_SETTINGS_ABOUT_COMPANY_TEL, settings.getCompanyTel());
        this.systemPropertyService.modifySysProp(APP_SETTINGS_ABOUT_COMPANY_ADDRESS, settings.getCompanyAddress());
        this.systemPropertyService.modifySysProp(APP_SETTINGS_ABOUT_COMPANY_EMAIL, settings.getCompanyEmail());
        this.systemPropertyService.modifySysProp(APP_SETTINGS_ABOUT_COMPANY_URL, settings.getCompanyUrl());
    }
}
